package com.lmb.todo.dao;

import com.lmb.todo.pojo.User;
import org.apache.ibatis.annotations.*;

/**
 * @author lmb
 */
@Mapper
public interface UserDao {

    /**
     * 根据 id 查找用户
     * @param id Integer
     * @return User
     */
    @Select("select * from t_user where id=#{id}")
    User getById(Integer id);

    /**
     * 根据 username 和 password 查找用户
     * @param username String
     * @param password String
     * @return User
     */
    @Select("select * from t_user " +
            "where username=#{username} and password=#{password}")
    User getByUsernameAndPassword(String username, String password);

    /**
     * 根据 username 查找用户
     * @param username String
     * @return User
     */
    @Select("select * from t_user " +
            "where username=#{username}")
    User getByUsername(String username);

    /**
     * 新增用户
     * @param user User
     * @return Integer
     */
    @Insert("insert into t_user " +
            "(username, password, createTime, updateTime, nikeName, realName, phone, country, national, address, hobby, " +
            "job, email, signature) " +
            "values " +
            "(#{username}, #{password}, #{createTime}, null, #{nikeName}, #{realName}, #{phone}, #{country}, " +
            "#{national}, #{address}, #{hobby}, #{job}, #{email}, #{signature})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Integer add(User user);

    /**
     * 更新用户信息
     * @param user User
     * @return Integer
     */
    @Update("<script>" +
            "update t_user " +
            "<set> " +
            "<if test='username != null'> username=#{username}, </if> " +
            "<if test='password != null'> password=#{password}, </if> " +
            "<if test='createTime != null'> createTime=#{createTime}, </if> " +
            "<if test='updateTime != null'> updateTime=#{updateTime}, </if> " +
            "<if test='nikeName != null'> nikeName=#{nikeName}, </if> " +
            "<if test='realName != null'> realName=#{realName}, </if> " +
            "<if test='phone != null'> phone=#{phone}, </if> " +
            "<if test='country != null'> country=#{country}, </if> " +
            "<if test='national != null'> national=#{national}, </if> " +
            "<if test='address != null'> address=#{address}, </if> " +
            "<if test='hobby != null'> hobby=#{hobby}, </if> " +
            "<if test='job != null'> job=#{job}, </if> " +
            "<if test='email != null'> email=#{email}, </if> " +
            "<if test='signature != null'> signature=#{signature} </if> " +
            "</set> " +
            "where id=#{id}" +
            "</script>")
    Integer update(User user);

    /**
     * 根据 id 删除用户
     * @param id Integer
     * @return Integer
     */
    @Delete("delete from t_user where id=#{id}")
    Integer deleteById(Integer id);
}
